package com.springboot.myweb.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
public class DruidConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource(){
        return new DruidDataSource();
    }

    //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //进入监控页面
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //后台需要账号密码
        HashMap<String,String> initParameters = new HashMap<>();
        initParameters.put("loginUsername","admin");//loginUsername固定
        initParameters.put("loginPassword","admin");//loginPassword固定

        //允许访问者
        initParameters.put("allow","");

        //禁止访问者 initParameters.put("jack","192.168.132.115");


        bean.setInitParameters(initParameters);//初始化
        return bean;
    }

}